# 64. 求幸存数之和
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function(line) {
if (!this.nums) {
this.nums = line.split(',').map(Number);
} else if (!this.jump) {
this.jump = Number(line);
} else if (!this.left) {
this.left = Number(line);
console.log(sumOfLeft(this.nums, this.jump, this.left));
}
});
function sumOfLeft(nums, jump, left) {
if (left >= nums.length) {
return nums.reduce((acc, val) => acc + val, 0);
}
let list = nums.slice();
let index = 0;
while(list.length > left) {
index = (index + jump + 1) % list.length;
list.splice(index, 1);
// 删除元素后,列表会变短,下一个起跳点应该向前移动一位
index = index - 1;
}
return list.reduce((acc, val) => acc + val, 0);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31